package com.pipi.controller;

import java.util.List;
import java.util.Map;

import com.pipi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pipi.dao.pojo.Disk;
import com.pipi.entity.Result;
import com.pipi.service.DiskService;
import com.pipi.utils.jedis.JedisClientPool;
import com.pipi.utils.json.JsonUtils;

@Controller
@RequestMapping("/disk")
public class DiskController {

	@Autowired
	private DiskService diskService;

	@Autowired
	private JedisClientPool jedisClient;

	@Autowired
	private UserService userService;

	private final String DISKCACHENAME = "DISKCACHENAME";



	/**
	 * 根据菜单ID获取对应的磁盘列表
	 * 首先尝试从Redis缓存中获取数据，如果缓存中没有，则从数据库中查询，并将结果缓存到Redis中
	 *
	 * @param menuId 菜单ID，用于识别特定菜单的磁盘列表
	 * @return 返回包含磁盘列表的Result对象
	 */
	@RequestMapping("/getMenuDisk")
	@ResponseBody
	public Result getDisksByMenuId(Integer menuId) {
//		System.out.println("getMenuDisk接口被调用");
//	    // 从Redis缓存中获取指定菜单ID对应的磁盘信息JSON字符串
	    String json = jedisClient.get(DISKCACHENAME + ":" + menuId);

	    List<Disk> menuDisk = null;

	    // 如果缓存中存在磁盘信息，则解析JSON字符串为Disk对象列表
	    if (null != json) {
	        menuDisk = JsonUtils.jsonToList(json, Disk.class);
//			System.out.println("从Redis缓存中获取的菜单ID为：" + menuId + "的磁盘信息为：" + menuDisk);
	    } else {
	        // 如果缓存中不存在磁盘信息，则从数据库中查询磁盘列表
	        menuDisk = diskService.getDisksByMenuId(menuId);
	        // 将查询到的磁盘列表转换为JSON字符串，并存入Redis缓存
	        jedisClient.set(DISKCACHENAME + ":" + menuId, JsonUtils.objectToJson(menuDisk));
	    }

	    // 返回包含磁盘列表的Result对象
	    return Result.ok(menuDisk);
	}

	/**
	 * 根据菜的id得到菜的详情
	 * 
	 * @param diskId
	 * @return
	 */
	@RequestMapping("/getDiskById")
	@Cacheable(value = "DISK",key="'diskId_'+#diskId")
	@ResponseBody
	public Result getDiskById(String diskId) {
		Integer id = Integer.parseInt(diskId);
		Disk disk = diskService.getDiskById(id);
		return Result.ok(disk);
	}
 
	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> getDiskList(@RequestParam(defaultValue = "0") Integer menuId, Integer page,
			Integer rows, String title) {
		Map<String, Object> map = diskService.getDiskList(menuId, page, rows, title);
		return map;
	}

	@RequestMapping("/delete")  
	@ResponseBody
	public Result deleteByDiskIds(String ids) {
		diskService.deleteByDiskIds(ids);
		// 更新Redis缓存并设置过期时间为1小时
		jedisClient.setex(DISKCACHENAME + ":" + ids, 3600, JsonUtils.objectToJson(ids));
		return Result.ok();
	}

	@RequestMapping("/save")
	@ResponseBody
	public Result save(Disk disk) {
		diskService.save(disk);
		// 更新Redis缓存并设置过期时间为1小时
		jedisClient.setex(DISKCACHENAME + ":" + disk.getId(), 3600, JsonUtils.objectToJson(disk));
		return Result.ok();
	}

	@RequestMapping("/update")
	@ResponseBody
	public Result update(Disk disk) {
		diskService.update(disk);
		// 更新Redis缓存并设置过期时间为1小时
		jedisClient.setex(DISKCACHENAME + ":" + disk.getId(), 3600, JsonUtils.objectToJson(disk));
		return Result.ok();
	}

	/**
	 * 上架
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("/up")
	@ResponseBody
	public Result up(String ids) {
		diskService.up(ids);
		return Result.ok();
	}

	/**
	 * 下架
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("/down")
	@ResponseBody
	public Result down(String ids) {
		diskService.down(ids);
		return Result.ok();
	}


	/**
	 * 获取推荐菜品菜单
	 */
	@RequestMapping("/getRecommendDisk")
	@ResponseBody
	public Result getRecommendDisk(@RequestParam String openId) {
		System.out.println("getRecommendDisk接口被调用");
		System.out.println("openId:"+openId);
		List<Disk> recommendDisk =userService.getRecommendDisk(openId);
		return Result.ok(recommendDisk);
	}
}


